package webRespones

import (
	"C_ProblemWorkOrder/problems"
	"net/http"

	//"C_ProblemWorkOrder/model"
	"C_ProblemWorkOrder/users"
	"fmt"
	"github.com/cht9862/mango/myloger"
	"github.com/gin-gonic/gin"
	"strconv"
)

var logFmt = myloger.NewLog()
var u = users.User{}
var u1 = users.SetUser{
	User: users.User{},
}



const (
	trueStatus = 200 // 成功状态
	falseStatus = 420 // 失败状态
)

// 示例请求处理函数
func SayHello(c *gin.Context) {
	// 返回JSON 格式的内容，gin.H就是一个简单的map[string]interface类型
	// c.JSON(statusCode, JSON) 将状态码和json返回给浏览器
	c.JSON(200, gin.H{
		"msg": "Hello Golang!",
	})
}


//用户相关
// -------------------------------------------- //
// 注册用户
func CreateUser(c *gin.Context) {
	u.Name = c.PostForm("username")
	u.Passwd = c.PostForm("password")
	u.Phone = c.PostForm("phone")
	logFmt.Info(u.Phone)
	logFmt.Info("执行到这里")

	if len(u.Phone) != 11{
		c.JSON(falseStatus, gin.H{
			"requestStatus": falseStatus,
			"describe": "不符合电话号码规则",
		})

	} else {
		err := u.AddUser()
		if err != nil {
			logFmt.Error(fmt.Sprintf("%v",err))
			c.JSON(falseStatus, gin.H{
				"requestStatus": falseStatus,
				"describe": err,
			})
		} else {
			c.JSON(trueStatus, gin.H{
				"AddUserStatus": trueStatus,
			})
		}
	}
}

// 修改用户信息
func SetUser(c *gin.Context) {
	u1.Name = c.Query("username")
	u1.Passwd = c.Query("password")
	u1.Phone = c.Query("phone")
	u1.OldName=c.Query("oldname")

	if len(u1.Phone) != 11{
		c.JSON(falseStatus, gin.H{
			"requestStatus": falseStatus,
			"describe": "不符合电话号码规则",
		})
	}else {
		res ,err := strconv.Atoi(u1.Phone)

		if err != nil{
			c.JSON(falseStatus, gin.H{
				"requestStatus": falseStatus,
				"describe": "~不符合电话号码规则",
			})
		}
		err = M.SetUser(u1.Name,u1.Passwd,u1.OldName, res)
		if err != nil {
			c.JSON(falseStatus, gin.H{
				"requestStatus": falseStatus,
				"describe": err,
			})
		}
	}
}

// -------------------------------------------- //



//问题相关
// -------------------------------------------- //

func AddProblems(c *gin.Context) {
	err := problems.AddProblem()
	if err != nil {
		c.JSON(http.StatusBadGateway,gin.H{
			"requestStatus": http.StatusBadGateway,
			"describe": err,
		})
	}else {
		c.JSON(http.StatusOK,gin.H{
			"requestStatus": http.StatusOK,
			"describe": "增加问题成功",
		})
	}
}

func DelProblems(c *gin.Context) {
	err := problems.DelProblem()
	if err != nil {
		c.JSON(http.StatusBadGateway,gin.H{
			"requestStatus": http.StatusBadGateway,
			"describe": err,
		})
	}else {
		c.JSON(http.StatusOK,gin.H{
			"requestStatus": http.StatusOK,
			"describe": "删除问题成功",
		})
	}
}

func SetProblems(c *gin.Context) {
	err := problems.SetProblem()
	if err != nil {
		c.JSON(http.StatusBadGateway,gin.H{
			"requestStatus": http.StatusBadGateway,
			"describe": err,
		})
	}else {
		c.JSON(http.StatusOK,gin.H{
			"requestStatus": http.StatusOK,
			"describe": "修改问题成功",
		})
	}
}


func GetProblems(c *gin.Context) {
	err := problems.GetProblem()
	if err != nil {
		c.JSON(http.StatusBadGateway,gin.H{
			"requestStatus": http.StatusBadGateway,
			"describe": err,
		})
	}else {
		c.JSON(http.StatusOK,gin.H{
			"requestStatus": http.StatusOK,
			"describe": "读取问题成功",
		})
	}
}
//func SayHello(c *gin.Context) {}
//func SayHello(c *gin.Context) {}